﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Manager;
using Model.Manager.DataBase;
using Model.Platform.PhysicalData;

namespace BML.Infrastructure.Implement
{
    public class ManagerOp
    {

        /// <summary>
        ///     新构建对象，属性按照名称倒序输入
        /// AccountName
        /// Avatar     
        /// CreateTime 
        /// Id         
        /// Name       
        /// Password   
        /// 
        /// </summary>
        /// <param name="props"></param>
        /// <returns></returns>
        public ManagerAccount NewManagerAccount(params Object[] props)
        {
            return new ManagerAccount
            {
                AccountName = (String)props[0],
                Avatar = (String)props[1],
                CreateTime = (DateTime)props[2],
                Id = (Guid)props[3],
                Name = (String)props[4],
                Password = (String)props[5],
                RoleType = (Int32)props[6],
            };
        }

        public ManagerAccount QueryAccount(String account, String password)
        {
            using (IDbContext db = Model.Manager.ContextFactory.GetPlatformConnect())
            {
                return db.Queryable<ManagerAccount>().SingleOrDefault(m => m.AccountName == account && m.Password == password);
            }
        }

        public void CreateAdministrater(String account, String password, String name, Int32 roleType)
        {
            using (IDbContext db = ContextFactory.GetPlatformConnect())
            {
                if (!db.Queryable<ManagerAccount>().Any(m => m.AccountName == account))
                {
                    CreateManagerAccount(db, account, password, name, roleType);
                    db.SaveAllChange();
                }
            }
        }

        private void CreateManagerAccount(IDbContext db, String account, String password, String name, Int32 roleType)
        {
            db.GetOperator<ManagerAccount>().Create(NewManagerAccount(account, "", DateTime.Now, Guid.NewGuid(), name, password, roleType));
        }

        public void CreateManagerAccount(String account, String password, String name, Int32 roleType)
        {
            using (IDbContext db = ContextFactory.GetPlatformConnect())
            {
                CreateManagerAccount(db, account, password, name, roleType);
                db.SaveAllChange();
            }
        }

    }
}
